package com.yfkj.common.message.handler.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.yfkj.common.message.domain.SmsRecord;
import com.yfkj.common.message.domain.TaskInfo;
import com.yfkj.common.message.domain.model.SmsContentModel;
import com.yfkj.common.message.domain.sms.MessageTypeSmsConfig;
import com.yfkj.common.message.domain.sms.SmsParam;
import com.yfkj.common.message.dto.account.sms.SmsAccount;
import com.yfkj.common.message.enums.ChannelType;
import com.yfkj.common.message.handler.BaseHandler;
import com.yfkj.common.message.handler.Handler;
import com.yfkj.common.message.script.SmsScript;
import com.yfkj.common.message.utils.AccountUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


/**
 * 短信发送处理
 */
@Component
@Slf4j
public class SmsHandler extends BaseHandler implements Handler {



    public SmsHandler() {
        channelCode = ChannelType.SMS.getCode();
    }

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private AccountUtils accountUtils;

    @Value("${yfkj.message.accountConfig.smsAccount}")
    private String accountConfig;


    @Override
    public boolean handler(TaskInfo taskInfo) {
        SmsContentModel smsContentModel =  (SmsContentModel)taskInfo.getContentModel();
        SmsParam smsParam = SmsParam.builder()
                .phones(taskInfo.getReceiver())
                .content(getSmsContent(taskInfo))
                .param(getSmsParam(taskInfo))
                .templateId(smsContentModel.getTemplateId())
                .build();
        try {
            /**
             * 发送短信
             */
            SmsAccount account = accountUtils.getAccount(accountConfig, SmsAccount.class);
            List<SmsRecord> recordList = applicationContext.
                    getBean(account.getScriptName(), SmsScript.class).
                    send(smsParam);
            if (CollUtil.isNotEmpty(recordList)) {
                return true;
            }
        } catch (Exception e) {

        }
        return false;
    }

    /**
     * 如果有输入链接，则把链接拼在文案后
     * <p>
     * PS: 这里可以考虑将链接 转 短链
     * PS: 如果是营销类的短信，需考虑拼接 回TD退订 之类的文案
     */
    private String getSmsContent(TaskInfo taskInfo) {
        SmsContentModel smsContentModel = (SmsContentModel) taskInfo.getContentModel();
        if (StrUtil.isNotBlank(smsContentModel.getUrl())) {
            return smsContentModel.getContent() + StrUtil.SPACE + smsContentModel.getUrl();
        } else {
            return smsContentModel.getContent();
        }
    }

    /**
     * 如果使用模板发送，需要用消息模板数据
     */
    private Map<String, String> getSmsParam(TaskInfo taskInfo) {
        SmsContentModel smsContentModel = (SmsContentModel) taskInfo.getContentModel();
        return smsContentModel.getParam();
    }


}
